package bjut.zrpc.core.optimization.event;

import bjut.zrpc.core.optimization.beanPostProcessor.ZRpcApplicationContextAware;
import bjut.zrpc.core.optimization.factory.SingletonFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @Author: zhuzhiming
 * @Date: 2022/7/2 23:51
 */
@Component
public class SpringRefreshListener implements ApplicationListener<ContextRefreshedEvent> {

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        SimpleZRpcEventMulticaster simpleZRpcEventMulticaster = SingletonFactory.getInstance(SimpleZRpcEventMulticaster.class);
        Map<String, ZRpcEventListener> beans = ZRpcApplicationContextAware.getBeansOfType(ZRpcEventListener.class);
        for (ZRpcEventListener zRpcListener : beans.values()) {
            simpleZRpcEventMulticaster.addZRpcEventListener((ZRpcEventListener<?>) zRpcListener);
        }
    }
}
